module ImmediateExtractor (
	input [31:0] instruction,
	input [2:0] selection,
	output reg signed [31:0] value
);

	initial begin
		value <= 'b0;
	end

	wire [11:0] imm_11_0	= instruction[31:20];
	wire [19:0] imm_31_12	= instruction[31:12];
	wire [4:0] imm_4_0		= instruction[11:7];
	wire [6:0] imm_11_5		= instruction[31:25];
	wire imm_11_B			= instruction[7];
	wire [3:0] imm_4_1		= instruction[11:8];
	wire [5:0] imm_10_5		= instruction[30:25];
	wire imm_12 			= instruction[31];
	wire [7:0] imm_19_12	= instruction[19:12];
	wire imm_11_J			= instruction[20];
	wire [9:0] imm_10_1		= instruction[30:21];
	wire imm_20				= instruction[31];

	wire signed [31:0] imm_I		= { {32{imm_11_0[11]} }, imm_11_0};
	wire signed [31:0] imm_U		= { {32{imm_31_12[19]} }, imm_31_12, 12'h000};
	wire signed [31:0] imm_B 		= { {32{imm_12}}, imm_11_B, imm_10_5, imm_4_1, 1'b0};
	wire signed [31:0] imm_S 		= { {32{imm_11_5[6]}}, imm_11_5, imm_4_0};
	wire signed [31:0] imm_J		= { {32{imm_20}}, imm_19_12, imm_11_J, imm_10_1, 1'b0};

	always @ (*) begin
		case(selection)
			1: value = imm_I;
			2: value = imm_U;
			3: value = imm_S;
			4: value = imm_B;
			5: value = imm_J;
			default: value = 0;
		endcase
	end
endmodule